package com.app.controller;

import com.app.service.IHuibenHomeworkService;
import com.app.vo.HomeworkDetailVO;
import com.app.vo.HomeworkInfoVO;
import com.app.vo.HomeworkUserVO;
import com.app.vo.BookVO;
import com.app.vo.CreateHomeworkDTO;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * <p>
 * 作业表 前端控制器
 * </p>
 *
 * @author lizi
 * @since 2025-07-29
 */
@RestController
@RequestMapping("/app-api/homework")
public class HuibenHomeworkController {
  
    @Autowired
    private IHuibenHomeworkService homeworkService;

    /**
     * 获取作业详情
     * @param homeworkId 作业ID
     * @param classId 班级ID
     * @return 作业详情
     */
    @GetMapping("/detail")
    public CommonResult<HomeworkDetailVO> getHomeworkDetail(
        @RequestParam("homeworkId") Long homeworkId,
        @RequestParam("classId") Long classId
    ) {
        HomeworkDetailVO detail = homeworkService.getHomeworkDetail(homeworkId, classId);
        if (detail == null) {
            return CommonResult.success(null);
        }
        return CommonResult.success(detail);
    }

    /**
     * 获取班级下所有作业及完成人数（分页）
     */
    @GetMapping("/list")
    public CommonResult<List<HomeworkInfoVO>> getHomeworkList(
        @RequestParam("classId") Long classId,
        @RequestParam("page") Integer page,
        @RequestParam("pageSize") Integer pageSize
    ) {
        List<HomeworkInfoVO> result = homeworkService.getHomeworkList(classId, page, pageSize);
        return CommonResult.success(result);
    }

    /**
     * 获取已完成用户列表
     */
    @GetMapping("/completed-users")
    public CommonResult<List<HomeworkUserVO>> getCompletedUsers(
        @RequestParam("homeworkId") Long homeworkId,
        @RequestParam("classId") Long classId
    ) {
        List<HomeworkUserVO> result = homeworkService.getCompletedUsers(homeworkId, classId);
        return CommonResult.success(result);
    }

    /**
     * 获取未完成用户列表
     */
    @GetMapping("/uncompleted-users")
    public CommonResult<List<HomeworkUserVO>> getUncompletedUsers(
        @RequestParam("homeworkId") Long homeworkId,
        @RequestParam("classId") Long classId
    ) {
        List<HomeworkUserVO> result = homeworkService.getUncompletedUsers(homeworkId, classId);
        return CommonResult.success(result);
    }

    /**
     * 根据书籍ID数组批量获取书籍详情
     */
    @PostMapping("/book/batch")
    public CommonResult<List<BookVO>> getBookDetailByIds(@RequestBody List<Long> bookIds) {
        List<BookVO> result = homeworkService.getBookDetailByIds(bookIds);
        return CommonResult.success(result);
    }

    /**
     * 获取绘本列表（分页）
     */
    @PostMapping("/book/list")
    public CommonResult<Map<String, Object>> getBookList(@RequestBody Map<String, Object> params) {
        Integer page = (Integer) params.get("page");
        Integer pageSize = (Integer) params.get("pageSize");
        String keyword = (String) params.get("keyword");
        
        Map<String, Object> result = homeworkService.getBookList(page, pageSize, keyword);
        return CommonResult.success(result);
    }

    /**
     * 创建作业
     */
    @PostMapping("/create")
    public CommonResult<Map<String, Object>> createHomework(@RequestBody CreateHomeworkDTO createHomeworkVO) {
        Long homeworkId = homeworkService.createHomework(createHomeworkVO);
        Map<String, Object> result = new HashMap<>();
        result.put("homeworkId", homeworkId);
        return CommonResult.success(result);
    }
}
